/**
 * Created by zenoniaonline on 14. 4. 8.
 */


var jade = require('jade');
var util = require('util');

exports.Real_showTables = function (req, res) {
    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("show Tables;");

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/Real/showTables', { type: "REAL SERVER", server: "REAL SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Real_showTable = function(req, res){

    var tableName = req.param('tableName');

    var DBPool = req.app.get('Real_Game_Backup');
    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from %s;", tableName);
        console.log("query : " + strQuery);

        conn.query(strQuery, function (err, rows, fields) {
            if (err) {
                console.log(err);
            } else {
                console.log(rows);
                res.render('jjaehuny/layout', { type: "REAL SERVER", rows: rows, fields: fields });
            }
            conn.release();
        })
    })
};

exports.Real_showCurrentUser = function(req, res){

    var DBPool = req.app.get('Real_Log_Backup');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from serveraddr_ccu;");

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                console.log(rows);
                res.render('jjaehuny/layout', { type:"REAL SERVER",server:"REAL SERVER", rows:rows, fields:fields });
            }
        })
    })
};

exports.Real_find_info_login_socialid = function(req, res) {

    var socialid = req.param('socialid');
    console.log(socialid);

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_login where socialid = '%s';", socialid);

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type: "REAL SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Real_find_info_login_seq = function(req, res) {

    var seq = req.param('seq');
    console.log(seq);

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_login where seq = %s;", seq);

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
            }
        })
    })
};


exports.Real_find_info_user_socialid = function(req, res){

    var socialid = req.param('socialid');

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_user as a inner join info_login as b on a.user_seq = b.seq where b.socialid = '%s';", socialid);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Real_find_info_user_seq = function(req, res){

    var seq = req.param('seq');

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_user where user_seq = '%s';", seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
            }
        })
    })

};

exports.Real_find_info_user_nickname = function(req, res){

    var nickname = req.param('nickname');

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_user where nickname = '%s';", nickname);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Real_find_info_character_user_seq = function(req, res){

    var user_seq = req.param('user_seq');

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_character where user_seq = '%s';", user_seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Real_find_info_character_seq = function(req, res){

    var seq = req.param('seq');

    var DBPool = req.app.get('Real_Game_Backup');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_character where seq = '%s';", seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
            }
        })
    })
};


exports.Real_update_env_block = function(req, res){

    var block = req.param('block');

    var DBPool = req.app.get('Real_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("update env_config set isBlock = %s;", block);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
                res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
            } else {
                console.log(rows);
                //alert("success");
                res.render('jjaehuny/Result', { title: 'Express', result:"success" });
            }
        })
    })
};


exports.Real_update_env_block = function(req, res){

    var block = req.param('block');

    var DBPool = req.app.get('Real_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("update env_config set isBlock = %s;", block);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
                res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
            } else {
                var strQuery = util.format("select seq, service_url, isBlock from env_config");
                conn.query(strQuery, function(err, rows, fields) {
                    conn.release();
                    if (err) {
                        console.log(err);
                    } else {
                        res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                    }
                })
            }
        })
    })
};


exports.Real_update_contents_open = function(req, res){
    var content = req.param('content');
    var onoff = req.param('onoff');

    var DBPool = req.app.get('Real_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("update resource_contents_open set isOpen = %s where contents = %s;", onoff, content );

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err){
                console.log(err);
                res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
            } else {
                var strQuery = util.format("select * from resource_contents_open");
                conn.query(strQuery, function(err, rows, fields) {
                    conn.release();
                    if (err) {
                        console.log(err);
                        res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                    } else {
                        res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                    }
                })
            }
        })
    })
};

exports.Real_lastlogin_to_buyitem = function (req, res) {

    var conn = req.app.get('Real_Game_Backup');

    var start=req.body.lastloginDate + ' 00:00:00',
        end=req.body.lastloginDate + ' 23:59:59';

    conn.query('SELECT c.USER_SEQ, c.UUID, c.USER_ID, c.COMPANY, SUM(c.PRICE) AS PRICE FROM ( ' +
        'SELECT seq FROM info_login a ' +
        'WHERE a.last_login_time BETWEEN ? AND ? AND a.unregister_time = 0 AND a.isBlck = 0)b, ' +
        'buyitemdata c ' +
        'WHERE b.seq = c.USER_SEQ and c.`STATUS` >= 1 ' +
        'GROUP BY c.USER_SEQ order by price', [start, end], function (err, rows, fields) {
        if (err) throw err;
        console.log(start);
        console.log(end);
        res.render('jjaehuny/layout', { loginDate: req.body.lastloginDate, rows: rows, fields: fields });
    });
};

